﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.PocketOutlook;

namespace CrashReporter
{
    public partial class frmMain : Form
    {
        private OutlookSession outlookSession;
        private EmailAccountCollection emailAccounts;
        private string filename;
        private string exceptionText;

        public frmMain() : this("")
        {
        }

        public frmMain(string filename)
        {
            InitializeComponent();

            this.outlookSession = new OutlookSession();
            this.emailAccounts = outlookSession.EmailAccounts;
            this.filename = filename;

        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + filename))
            {
                exceptionText = sr.ReadToEnd();
            }

            System.IO.File.Delete(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + filename);

            foreach (EmailAccount emailAccount in emailAccounts)
            {
                comboBoxEmailAccount.Items.Add(emailAccount.Name);
            }
        }

        private void menuItemSend_Click(object sender, EventArgs e)
        {
            if (comboBoxEmailAccount.SelectedItem != null)
            {
                EmailMessage emailMessage = new Microsoft.WindowsMobile.PocketOutlook.EmailMessage();
                emailMessage.BodyText = textBoxComments.Text + "\r\n\r\n" + exceptionText;
                emailMessage.To.Add(new Recipient("dirk.rettschlag@gmail.com"));
                emailMessage.Subject = "Departure Crash Report";

                string accountName = (string)comboBoxEmailAccount.SelectedItem;
                emailAccounts[accountName].Send(emailMessage);
                MessagingApplication.Synchronize(emailAccounts[accountName]);
                this.Close();
            }
        }

        private void menuItemClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}